package com.cdut.drivingschool.http;
import com.cdut.drivingschool.bean.BaseType;
import com.cdut.drivingschool.exception.ParserException;
import com.cdut.drivingschool.http.parser.Parser;
import com.cdut.drivingschool.listener.HttpListener;
import com.cdut.drivingschool.util.LogTool;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
public class HttpClient {
	private static final String BASE_URL = "http://202.115.137.220/api/";
	 public static void request(String url, RequestParams params, final Parser parser,final HttpListener listener){
		 HttpUtils httpUtils = new HttpUtils();
		 LogTool.i("url:  " + getAbsoluteUrl(url));
		 httpUtils.send(HttpMethod.POST, getAbsoluteUrl(url), params, new RequestCallBack<String>() {

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				try {
					LogTool.i("response:  " + responseInfo.result);
					String json = responseInfo.result;
					if(json != null && json.startsWith("\ufeff")){
						json = json.substring(1);
					}
					BaseType data = parser.parser(json);
					listener.onSuccess(data);
				} catch (ParserException e) {
					listener.onFail(e.getMessage());
				}
			}
			@Override
			public void onFailure(HttpException error, String msg) {
				listener.onFail("网络连接失败");
			}
		 });
	 }
	 private static String getAbsoluteUrl(String url){
		 return BASE_URL + url;
	 }
}

